//
//  MainTabBar.swift
//  Weibo
//
//  Created by 刘蒙 on 16/1/10.
//  Copyright © 2016年 liumeng. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(plusBtn)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //定义一个索引 index
        var index: CGFloat = 0
        let width = bounds.width / 5
        let height = bounds.height
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        
        
        
        //遍历子视图
        for subview in subviews {
            
            //获取所有的uitabbarbutton类型的对象
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            subview.frame = CGRectOffset(rect, index * width, 0)
            
            index += (index == 1 ? 2 : 1)
//                index++
            }
        }
        
        //设置+的位置
        //需要懒加载一个+
        plusBtn.frame = CGRectOffset(rect, 2 * width, 0)
    }
    
    lazy var plusBtn: UIButton = {
       
        //创建一个button对象
        let btn = UIButton()
        //设置button的背景图片,normal和highlighted
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        //设置按钮的图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        //让按钮自适应大小,懒加载是一个闭包,btn.
        btn.sizeToFit()
        return btn
    }()
    
    
}
